package org.dtrd.modules.service.entity.po;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.dtrd.config.pay.OrderUtil;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.service.entity.bean.ServicePackageInfo;

import java.util.Date;


/**
 * <p>
 * 服务包信息
 * </p>
 *
 * @author QR
 * @since 2021-11-19 18:52:54
 */
@Getter
@Setter
@TableName("dtrd_ent_rd_service_package")
public class DtrdEntRdServicePackage {

    @TableId(value = "data_id", type = IdType.AUTO)
    private Integer dataId;

    /**
     * 所属机构编码
     */
    private String orgCode;

    /**
     * 服务包名字
     */
    private String packageName;

    /**
     * 服务周期
     */
    private Integer packageServicePeriod;

    /**
     * 服务周期单位
     */
    private Integer periodUnit;

    /**
     * 服务包原价 单位分
     */
    private Integer packagePrice;

    /**
     * 服务包折扣价 单位分
     */
    private Integer packageDiscount;

    /**
     * 服务包详情
     */
    private String packageDetail;

    /**
     * 服务包缩略图
     */
    private String packageThumb;

    /**
     * 服务包描述
     */
    private String packageDesc;

    /**
     * 服务包类型
     */
    private Integer packageType;

    private String packageSlideshow;

    @TableField("manage_effect")
    private String manageEffect;

    private Date createTime;

    private Date updateTime;

    @TableLogic
    private Integer isDel;

    public DtrdEntRdServicePackage init() {
        isDel = 0;
        createTime = new Date();
        updateTime = new Date();
        return this;
    }

    public DtrdEntRdServicePackage parseFromDto(ServicePackageInfo dto) {
        dataId = dto.getDataId();
        packageName = dto.getPackageName();
        packageServicePeriod = dto.getPackageServicePeriod();
        packagePrice = wrappPrice(dto.getPackagePrice());
        packageDiscount = wrappPrice(dto.getPackageDiscount());
        packageSlideshow = dto.getPackageSlideshow();
        packageDetail = dto.getPackageDetail();
        packageDesc = dto.getPackageDesc();
        packageType = dto.getPackageType();
        packageThumb=dto.getPackageThumb();
        Integer packageType = dto.getPackageType();
        Constant.ServicePackageType PackageType = Constant.ServicePackageType.valueOf(packageType);
        switch (PackageType) {
            case EXPERIENCE:
                periodUnit = Constant.PeriodUnit.WEEK.getType();
                break;
            case NORMAL:
            case VIP:
                periodUnit = Constant.PeriodUnit.MONTH.getType();
                break;
            default:
                break;
        }
        manageEffect= JSONUtil.toJsonStr(dto.getManageEffect());
        return this;
    }

    private Integer wrappPrice(Float price) {
        if (price != null) {
            return OrderUtil.changeY2F(price);
        }
        return null;
    }

}
